package com.project.smms.controller;
import com.project.smms.pojo.User;
import com.project.smms.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 莫息涛
 * @Description: 登录相关的控制器
 * @date 2020/3/5 17:40
 */
@Controller
public class LoginController {

    @Autowired
    private UserService userService;

    @RequestMapping("/toLogin")
    public String toLogin(Model model){
        model.addAttribute("user",new User());
        return "login";
    }

    @RequestMapping("/checkUser")
    public String checkUser(@ModelAttribute User user, HttpServletRequest request){
        String userName = user.getUser_name();
        String passWord = user.getUser_passwd();
        List<User> users = userService.selectUser();
        for (User tempuser : users) {
            if (tempuser.getUser_name().equals(userName) & tempuser.getUser_passwd().equals(passWord)){
                HttpSession session = request.getSession();
                session.setAttribute("user",user);
                return "index";
            }
        }
        return "error";
    }
}
